﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Org.BouncyCastle.X509;
using Org.BouncyCastle.Crypto;

namespace iPdf
{
    class CertificateDropDownItem: TataDropDownItem
    {
        #region Fields

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public CertificateSource Source;
        public const string SystemCertificateSuffix = "[System]";
        private string password = string.Empty;
        
        private AsymmetricKeyParameter privateKey ;
        #endregion 

        public override string Text
        {
            get
            {
                return text + (Source == CertificateSource.System ? SystemCertificateSuffix : string.Empty);
            }
        }

        public override bool IsValid
        {
            get
            {
                switch (Source)
                {
                    case CertificateSource.File:
                        return File.Exists(Text);

                    case CertificateSource.System:
                        return SystemCertificateStore.Contains(Text);
                }

                return false;
            }
        }

        public string Password
        {
            set
            {
                password = value;
            }
        }

        public AsymmetricKeyParameter PrivateKey
        {
            get
            {
                GetCertificates();
                return privateKey;
            }
        }


        public CertificateDropDownItem(CertificateDropDown certDropDown)
            : base(certDropDown)
        {
            Source = CertificateSource.File;
        }

        public CertificateDropDownItem(CertificateDropDown certDropDown,
                string text, EventHandler clickHandler)
            : this(certDropDown, text, clickHandler, CertificateSource.File)
        {

        }

        public CertificateDropDownItem(CertificateDropDown certDropDown,
        string text, EventHandler clickHandler, CertificateSource source)
            : base(certDropDown, text, clickHandler)
        {
            Source = source;
        }

        public X509Certificate[] GetCertificates()
        {
            if (!IsValid)
            {
                return null;
            }

            List<X509Certificate> certificates = new List<X509Certificate>();
            switch (Source)
            {
                case CertificateSource.File:
                    try
                    {
                        PfxCertificate cert = new PfxCertificate(text, password);
                        certificates.AddRange(cert.Chain);
                        privateKey = cert.Akp;
                    }
                    catch
                    {
                        return null;
                    }
                    break;

                case CertificateSource.System:
                    certificates.Add( SystemCertificateStore.GetCertificate(text));
                    break;
            }

            return certificates.ToArray();
        }
    }
}
